package com.movitech.mobile.cz.base.supplier.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.IdGenUtils;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.supplier.entity.SSuppliersInspection;
import com.movitech.mobile.cz.base.supplier.entity.ext.ExtSSuppliersInspection;
import com.movitech.mobile.cz.base.supplier.mapper.SSuppliersInspectionMapper;
import com.movitech.mobile.cz.base.supplier.mapper.ext.ExtSSuppliersInspectionMapper;
import com.movitech.mobile.cz.base.supplier.service.SSuppliersInspectionService;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 供方考察报告 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@Service("sSuppliersInspectionService")
@Transactional
public class SSuppliersInspectionServiceImpl extends ServiceImpl<SSuppliersInspectionMapper, SSuppliersInspection> implements SSuppliersInspectionService {

	@Autowired
	private SSuppliersInspectionMapper sSuppliersInspectionMapper;

	@Autowired
	private ExtSSuppliersInspectionMapper extSSuppliersInspectionMapper;

	@Override
	public boolean addSSuppliersInspection(SSuppliersInspection sSuppliersInspection) {
		
		if(StringUtils.isEmpty(sSuppliersInspection.getId())){
			sSuppliersInspection.setId(IdGenUtils.uuid());
		}
		
		String userId = UserUtils.getCurrentUserId();
		sSuppliersInspection.setCreateBy(userId);
		if(sSuppliersInspection.getCreateDate()==null){
			Date createDate = new Date();
			sSuppliersInspection.setCreateDate(createDate);
			sSuppliersInspection.setUpdateDate(createDate);
		}
		sSuppliersInspection.setUpdateBy(userId);
		sSuppliersInspection.setDelFlag(CommonConstants.NO_INTEGER);
		
		int i = baseMapper.insert(sSuppliersInspection);
		
		return i > 0;
	}

    @Override
    public SSuppliersInspection getBySupplierId(String supplierId) {
		SSuppliersInspection sSuppliersInspection = sSuppliersInspectionMapper.getBySupplierId(supplierId);
		return sSuppliersInspection;
	}

	public List<ExtSSuppliersInspection> getListBySupplierId(String supplierId) {
		List<ExtSSuppliersInspection> sSuppliersInspection = extSSuppliersInspectionMapper.getListBySupplierId(supplierId);
		return sSuppliersInspection;
	}

	/**
	 * 根据供方id和地区公司id获取未处理最新考察报告记录
	 */
	@Override
	public SSuppliersInspection getLatestRecordBySuppliersIdAndRegionalCompanyId(
			String suppliersId, String regionalCompanyId) {
//		SSuppliersInspection sSuppliersInspection = new SSuppliersInspection();
//		sSuppliersInspection.setSuppliersId(suppliersId);
//		sSuppliersInspection.setRegionalCompanyId(regionalCompanyId);
//		sSuppliersInspection.setDoneFlag(0);
//		sSuppliersInspection.setDelFlag(0);
//		//sSuppliersInspection.setId("DPM20180626135622579GYC2M");
//		EntityWrapper entityWrapper = new EntityWrapper();
//		entityWrapper.setEntity(sSuppliersInspection);
		return extSSuppliersInspectionMapper.getLatestRecordBySuppliersIdAndRegionalCompanyId(suppliersId, regionalCompanyId);
		//return selectOne(entityWrapper);
	}

	@Override
	public SSuppliersInspection getUnDoneSuppliersInspectionBySupplierIdAndRegionCompanyId(String supplierId,
			String serviceRegionalCompanyId) {
		
		SSuppliersInspection param = new SSuppliersInspection();
		param.setSuppliersId(supplierId);
		param.setRegionalCompanyId(serviceRegionalCompanyId);
		param.setDoneFlag(CommonConstants.NO_INTEGER);
		
		return sSuppliersInspectionMapper.selectOne(param);
	}

}
